/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021.
 * Description: header for ubifs maintenance.
 * Author: liangfangping
 * Create: 2021-05-07
 */

#ifndef RTOS_UBIFS_DEBUG_H
#define RTOS_UBIFS_DEBUG_H

/* UBIFS enhance messages */
#ifdef CONFIG_RTOS_UBIFS_MAINTENANCE_ENHANCE
#include "ubifs.h"

#ifdef CONFIG_RTOS_UBIFS_KBOX
/* This func print ubi_num, vol_id, pid and error message */
#define rtos_ubifs_err(c, cond, fmt, ...)			\
	do {							\
		if (cond) {					\
			int kbox_ret;				\
			kbox_ret = set_kbox_region(get_flash_kbox_id());	\
			pr_err("UBIFS_ERR (ubi%d:%d pid %d): %s:%d " fmt "\n",	\
				(c)->vi.ubi_num, (c)->vi.vol_id, current->pid,	\
				__func__, __LINE__, ##__VA_ARGS__);       \
			restore_kbox_region(kbox_ret);		\
		}						\
	} while (0)

/* This func not print ubi_num, vol_id, pid */
#define rtos_ubifs_err_noc(cond, fmt, ...)			\
	do {							\
		if (cond) {					\
			int kbox_ret;				\
			kbox_ret = set_kbox_region(get_flash_kbox_id());	\
			pr_err("UBIFS_ERR %s:%d " fmt "\n",	\
				__func__, __LINE__, ##__VA_ARGS__);       \
			restore_kbox_region(kbox_ret);		\
		}						\
	} while (0)
/* This func print ubifs message */
#define rtos_ubifs_msg(fmt, ...)				\
	do {							\
		int kbox_ret;					\
		kbox_ret = set_kbox_region(get_flash_kbox_id());	\
		pr_notice("UBIFS_MSG %s:%d " fmt "\n",		\
			__func__, __LINE__, ##__VA_ARGS__);	\
		restore_kbox_region(kbox_ret);			\
	} while (0)
#else
/* This func print ubi_num, vol_id, pid and error message */
#define rtos_ubifs_err(c, cond, fmt, ...)				\
	do {							\
		if (cond) {					\
			pr_err("UBIFS_ERR (ubi%d:%d pid %d): %s:%d " fmt "\n",	\
			(c)->vi.ubi_num, (c)->vi.vol_id, current->pid,	\
				__func__, __LINE__, ##__VA_ARGS__);	\
		}						\
	} while (0)
/* This func not print ubi_num, vol_id, pid */
#define rtos_ubifs_err_noc(cond, fmt, ...)				\
	do {							\
		if (cond) {					\
			pr_err("UBIFS_ERR %s:%d " fmt "\n",	\
				__func__, __LINE__, ##__VA_ARGS__);	\
		}						\
	} while (0)
/* This func print ubifs message */
#define rtos_ubifs_msg(fmt, ...)				\
	pr_notice("UBIFS_MSG  %s:%d " fmt "\n",      \
		__func__, __LINE__, ##__VA_ARGS__)
#endif /* end of CONFIG_RTOS_UBIFS_KBOX */
/* This func print line num where the error happened */
#define rtos_ubifs_err_set(cond, err_cnt)			\
	do {							\
		if (cond) {					\
			err_cnt = __LINE__;				\
		}						\
	} while (0)

void ubifs_dump_ubifs_info(struct ubifs_info *c);
void ubifs_dump_budg_info(struct ubifs_info *c);
void ubifs_dump_ubifs_lpt_info(const struct ubifs_info *c);
#else /* else of CONFIG_RTOS_UBIFS_MAINTENANCE_ENHANCE */
#define rtos_ubifs_err(c, cond, fmt, ...)
#define rtos_ubifs_err_noc(cond, fmt, ...)
#define rtos_ubifs_msg(fmt, ...)
#define rtos_ubifs_err_set(cond, err_cnt)
static inline void ubifs_dump_ubifs_info(struct ubifs_info *c) {}
static inline void ubifs_dump_budg_info(struct ubifs_info *c) {}
static inline void ubifs_dump_ubifs_lpt_info(const struct ubifs_info *c) {}
#endif /* end of CONFIG_RTOS_UBIFS_MAINTENANCE_ENHANCE */
#endif /* end of RTOS_UBIFS_DEBUG_H */
